package oss.juelg.flashlight_mini;

import oss.juelg.flashlight_mini.FlashState.FlashMode;
import oss.juelg.flashlight_mini.FlashState.ReleasePolicy;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

/**
 * Copyright 2012 Chris Juelg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author Chris Juelg
 * @see http://code.google.com/p/flashlight-mini/
 * 
 * XXX face unlock problem: if flash when screen locked, face unlock cannot work
 * XXX notification für licht an, klick entfernt flash (siehe TeslaLED)
 */
public class FlashlightActivity extends Activity implements
		OnCheckedChangeListener {
	public static final String TAG = "flashlight";

	// flash will definitely be active after sending START_INTENT
	public static final String START_INTENT_STRING = "oss.juelg.flashlight_mini.START_INTENT";
	public static final Intent START_INTENT = new Intent(START_INTENT_STRING);

	// only ever used to toggle
	public static final String FLASH_INTENT_STRING = new String(
			"oss.juelg.flashlight_mini.FLASH_INTENT");

	public static final Intent TOGGLE_INTENT = new Intent(FLASH_INTENT_STRING);

	public static final String RELEASE_POLICY_INTENT_STRING = "oss.juelg.flashlight_mini.RELEASE_POLICY_INTENT";

	private static final Intent EAGER_INTENT = new Intent(
			RELEASE_POLICY_INTENT_STRING);

	private static final Intent LAZY_INTENT = new Intent(
			RELEASE_POLICY_INTENT_STRING);

	// use putExtra instead of uri?
	static {
		TOGGLE_INTENT.putExtra("flash", FlashMode.TOGGLE);
		EAGER_INTENT.putExtra("policy", ReleasePolicy.EAGER);
		LAZY_INTENT.putExtra("policy", ReleasePolicy.LAZY);
	}

	private ToggleButton toggle;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.d(TAG, "onCreate");

		toggle = (ToggleButton) findViewById(R.id.toggleButtonActivity);
		toggle.setOnCheckedChangeListener(this);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		// start service, enable flash
		startService(START_INTENT);

		Log.v(TAG, "lazy intent action: " + LAZY_INTENT.getAction());
		// Log.v(TAG, "lazy intent data: " + LAZY_INTENT.getDataString());
		// Log.v(TAG, "lazy uri: " + LAZY_URI.toString());
		// Log.v(TAG, "lazy uri scheme: " + LAZY_URI.getScheme());
		// Log.v(TAG,
		// "lazy uri schemespecificpart: "
		// + LAZY_URI.getSchemeSpecificPart());

		Log.v(TAG, "toggle intent action: " + TOGGLE_INTENT.getAction());
		// Log.v(TAG, "toggle intent data: " + TOGGLE_INTENT.getDataString());
		// Log.v(TAG, "toggle uri: " + FLASH_TOGGLE_URI.toString());
		// Log.v(TAG, "toggle uri scheme: " + FLASH_TOGGLE_URI.getScheme());
		// Log.v(TAG,
		// "toggle uri schemespecificpart: "
		// + FLASH_TOGGLE_URI.getSchemeSpecificPart());

	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");

		startService(EAGER_INTENT);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");

		startService(LAZY_INTENT);
		// EXTEND: ask service for flash state?
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");

		// never stop service by ourself
		// boolean wasRunning = stopService(EXIT_INTENT);
		// Log.d(TAG, "onDestroy: service was running: "+wasRunning);

		if (toggle.isChecked()) {
			startService(TOGGLE_INTENT);
			startService(EAGER_INTENT);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton button, boolean isChecked) {
		Log.d(TAG, "onCheckedChanged, button:" + button + " checked:"
				+ isChecked);

		//TODO send isChecked instead of TOGGLE
		ComponentName retVal = startService(TOGGLE_INTENT);
		Log.d(TAG, "onCheckedChanged, startService() called using "
				+ TOGGLE_INTENT.toUri(0));
		Log.d(TAG,
				"onCheckedChanged, startService() called using "
						+ TOGGLE_INTENT.getAction() + ", "
						+ TOGGLE_INTENT.getDataString());
		Log.d(TAG, "onCheckedChanged, startService() retVal: "
				+ ((retVal == null) ? null : retVal.flattenToString()));
	}
}